<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/11/14
 * Time: 16:22
 */

namespace app\admin\controller;

/**
*标签控制器
 */
class Tag extends common
{
    protected $db;
    public function __construct()
    {
        parent::__construct();
        $this->db = new \app\common\model\Tag();
    }
    //标签主页
    public function index(){

        // 查询状态为1的用户数据 并且每页显示3条数据
        $data = db('tag')->order('tid desc')->paginate(3);
        // 把分页数据赋值给模板变量
        $this->assign('data', $data);
        //视图
        return view();
    }
    //添加、编辑标签标签
    public function add()
    {
        $tid = input('param.tid');
        if (!$tid == null) {
            //.获取所属分类数据
            $oldData = db('tag')->where('tid',$tid)->value('tname');
            if(request()->isPost()){
            $data = input('post.');
            $res = $this->db->update($data);
            if($res['valid']){
                $this->success($res['msg'],'index');
            }else{
                $this->error($res['msg']);
            }
        }

        } else {
            $oldData='';
            //添加标签
            if (request()->isPost()) {
                $data = input('post.');
                $res = $this->db->add($data);
                if ($res['valid']) {
                    $this->success($res['msg'], 'index');
                } else {
                    $this->error($res['msg']);
                }
            }
        }
        $this->assign('oldData',$oldData);
        //视图
        return view();
    }
}